package com.bua.toothfactory;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Base64;
import com.bua.control.ImageLableButton;
import com.bua.control.SingleCheckButton;
import com.bua.dao.DesignItem;
import com.bua.datafactory.OrderCreatedResultDataFactory;
import com.bua.datafactory.OrderDetailDataFactory;
import com.bua.net.NetConfig;
import com.bua.net.RequestTask;
import com.bua.net.ResponseReport;
import com.bua.net.Tool;
import com.bua.toothfactory.config.Config;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;

public class OrderDetailActivity extends Activity implements ResponseReport {

    private TextView tvOrderId;
    private TextView tvCreateDate;
    private Button btnCreateOrder;
    private EditText etName;
    private SingleCheckButton scbSex;
    private EditText etAge;
    private EditText etPhone;
    private SingleCheckButton scbDentist;
    private EditText etDtPhone;
    private TextView tvWtDate;
    private TextView tvDyDate;
    private GridView designListView;

    private OrderDetailDataFactory orderDetail = null;
    private String OrderId = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        Intent intent = this.getIntent();
        OrderId = intent.getStringExtra("orderId");

        btnCreateOrder = (Button) findViewById(R.id.btnCreateOrder);
        btnCreateOrder.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CreateOrder();
            }
        });
        tvOrderId = (TextView) findViewById(R.id.tvOrderId);
        tvCreateDate = (TextView) findViewById(R.id.tvCreateDate);
        etName = (EditText) findViewById(R.id.etName);
        scbSex = (SingleCheckButton) findViewById(R.id.scbSex);
        scbSex.SetItems(Config.sex);
        etAge = (EditText) findViewById(R.id.etAge);
        etPhone = (EditText) findViewById(R.id.etPhone);
        scbDentist = (SingleCheckButton) findViewById(R.id.scbDentise);
        scbDentist.items = Config.DentistList;
        etDtPhone = (EditText) findViewById(R.id.etDtPhone);
        tvWtDate = (TextView) findViewById(R.id.tvWtDate);
        tvDyDate = (TextView) findViewById(R.id.tvDyDate);
        designListView = (GridView) findViewById(R.id.designList);


        if (OrderId != null) {
            tvOrderId.setText("订单编号：" + OrderId);
            btnCreateOrder.setVisibility(View.INVISIBLE);
            etName.setFocusableInTouchMode(false);
            scbSex.setEnabled(false);
            etAge.setFocusableInTouchMode(false);
            etPhone.setFocusableInTouchMode(false);
            scbDentist.setEnabled(false);
            etDtPhone.setFocusableInTouchMode(false);
            etAge.setFocusableInTouchMode(false);
            GetOrderDetail(OrderId);


        }
    }
    @Override
    protected void onRestart()
    {
        super.onRestart();
        GetOrderDetail(OrderId);
    }
    private void GetOrderDetail(String OrderId) {
        String url = NetConfig.creatUrl(NetConfig.HttpRequestId.GET_ORDER_DETAIL);
        RequestTask request = new RequestTask();
        HashMap<String, String> requestData = NetConfig.GetOrderDetail(OrderId);
        request.CreateTask(url, requestData, NetConfig.HttpRequestId.GET_ORDER_DETAIL.ordinal(), this);
    }

    private void GetDentistList() {
        String url = NetConfig.creatUrl(NetConfig.HttpRequestId.GET_DENTIST_LIST);
        RequestTask request = new RequestTask();

        request.CreateTask(url, null, NetConfig.HttpRequestId.GET_DENTIST_LIST.ordinal(), this);
    }

    private void CreateOrder() {
        String patientName = etName.getText().toString();
        String patientSex = scbSex.GetTitle();
        String patientAge = etAge.getText().toString();
        String patientPhone = etPhone.getText().toString();
        String[] dentist = scbDentist.GetTitle().split(" ");
        String dentistName=null;
        String dentistId=null;
        if(dentist.length==2)
        {
        dentistName = dentist[1];
        dentistId = dentist[0];
        }
        String dentistPhone = etDtPhone.getText().toString();


        if (patientName != null &&
                patientSex != null &&
                patientAge != null &&
                patientPhone != null &&
                dentistName != null &&
                dentistId != null &&
                dentistPhone != null) {
            String url = NetConfig.creatUrl(NetConfig.HttpRequestId.CREATE_ORDER);
            RequestTask request = new RequestTask();
            HashMap<String, String> requestData = NetConfig.CreateOrder(
                    patientName, patientSex, patientAge, patientPhone, dentistId,
                    dentistName, dentistPhone);
            request.CreateTask(url, requestData, NetConfig.HttpRequestId.CREATE_ORDER.ordinal(), this);
        } else {
            Tool.showToast(this, "请输入完整信息");
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_order_detail, menu);
        return true;
    }

    public class DesignAdapter extends BaseAdapter {
        public Context mContext;
        public DesignItem[] designList = null;

        public DesignAdapter(Context context,
                             DesignItem[] DesignList) {
            super();
            mContext = context;
            designList = DesignList;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
//			if (OrderId == null)
//				return 0;
            if (designList == null)
                return 1;
            return designList.length + 1;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(final int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            if (arg0 == getCount() - 1) {
                LinearLayout itemLayout = new LinearLayout(mContext);
                itemLayout.setOrientation(LinearLayout.VERTICAL);
                Button CreateLDesinOrder = new Button(mContext);
                CreateLDesinOrder.setTextSize(14);
                CreateLDesinOrder.setText("创建活动修复订单");
                CreateLDesinOrder.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(OrderDetailActivity.this,
                                HuodongOrderActivity.class);
                        intent.putExtra("orderId", OrderId);
                        startActivity(intent);

                    }
                });
                itemLayout.addView(CreateLDesinOrder);
                Button CreateSDesinOrder = new Button(mContext);
                CreateSDesinOrder.setTextSize(14);
                CreateSDesinOrder.setText("创建固定修复订单");
                CreateSDesinOrder.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(OrderDetailActivity.this,
                                GudingOrderActivity.class);
                        intent.putExtra("orderId", OrderId);

                        startActivity(intent);

                    }
                });
                itemLayout.addView(CreateSDesinOrder);

                Button CreateZDesinOrder = new Button(mContext);
                CreateZDesinOrder.setTextSize(14);
                CreateZDesinOrder.setText("创建种植修复订单");
                CreateZDesinOrder.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(OrderDetailActivity.this,
                                ZhongzhitiOrderActivity.class);
                        intent.putExtra("orderId", OrderId);

                        startActivity(intent);

                    }
                });
                itemLayout.addView(CreateZDesinOrder);
                return itemLayout;
            } else {
                // 锟斤拷锟斤拷锟斤拷锟?
                ImageLableButton ilbDesign = new ImageLableButton(mContext);
                ilbDesign.setTitle(designList[arg0].approveStatus);
                ilbDesign.setBackgroundColor(Color.WHITE);
                byte[] data= Base64.decode(designList[arg0].designImage,Base64.DEFAULT);
                 Bitmap bm= BitmapFactory.decodeByteArray(data,0,data.length);
                // 获得图片的宽高
                int width = bm.getWidth();
                int height = bm.getHeight();
                // 设置想要的大小
                int newWidth = 120;
                int newHeight = 100;
                // 计算缩放比例
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                // 取得想要缩放的matrix参数
                Matrix matrix = new Matrix();
                matrix.postScale(scaleWidth, scaleHeight);
                // 得到新的图片
                Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
                        true);


				ilbDesign.setImage( newbm);


                ilbDesign.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO 自动生成的方法存根
                        String designId = designList[arg0].designId;

                        Intent intent = null;
                        intent = new Intent(OrderDetailActivity.this,
                                DesignDetailActivity.class);

                        intent.putExtra("designId", designId);
                        startActivity(intent);
                    }
                });
                return ilbDesign;
            }
        }

    }


    @Override
    public void callBack(String result, int requestId) {
        // TODO 自动生成的方法存根
        NetConfig.HttpRequestId RequestId = NetConfig.HttpRequestId.values()[requestId];
        switch (RequestId) {
            case GET_ORDER_DETAIL: {
                orderDetail = new OrderDetailDataFactory(result);
                if (orderDetail.status == 200) {
                    tvCreateDate.setText("创建日期:" + orderDetail.createdDate);
                    btnCreateOrder.setVisibility(View.INVISIBLE);
                    etName.setText(orderDetail.patientName);
                    scbSex.setText(orderDetail.patientSex);
                    etAge.setText(orderDetail.patientAge);
                    etPhone.setText(orderDetail.patientPhone);
                    scbDentist.setText(orderDetail.dentistName);
                    etDtPhone.setText(orderDetail.dentistPhone);
                    tvWtDate.setText(orderDetail.weituoDate);
                    tvDyDate.setText(orderDetail.daiyaDate);
                    designListView.setAdapter(new DesignAdapter(this,
                            orderDetail.designItem));
                } else {
                    Tool.showToast(this, orderDetail.message);
                }

            }

            break;
            case CREATE_ORDER: {
                OrderCreatedResultDataFactory OrderCreatedResult = new OrderCreatedResultDataFactory(result);
                if (OrderCreatedResult.status == 200) {
                    OrderId = OrderCreatedResult.orderId;
                    tvOrderId.setText("订单编号：" + OrderId);
                    tvCreateDate.setText("创建日期:" + OrderCreatedResult.createdDate);
                    btnCreateOrder.setVisibility(View.INVISIBLE);
                    designListView.setAdapter(new DesignAdapter(this, null));
                } else {
                    Tool.showToast(this, OrderCreatedResult.message);
                }

            }
            break;
            default:
                break;
        }
    }

}
